import { _decorator, geometry, Label, easing, Tween, assetManager, Camera, Node, Color, math, Sprite, SystemEvent, LabelComponent, SkinnedMeshRenderer, Prefab, v3, Vec3, SkeletalAnimation, ProgressBar, tween } from 'cc';

import BaseView from '../../Script/game/BaseView';
import { Bullet } from '../../Script/config/BaseData';
import Tool from '../../Script/tool/Tool'
// import { luckyBox } from './luckyBox';
import GD from '../../Script/config/GD';
import GDef from '../../Script/config/GDef';
const { bits } = math;
const { ccclass, property } = _decorator;

@ccclass('preCoinBar')
export class preCoinBar extends BaseView {
    @property({
        type: Node
    })
    nodeCoin: Node = null;
    @property({
        type: Label
    })
    labelCoinNum: Label = null;
    onLoad() {
        var self = this;
        this.labelCoinNum.string = Tool.formatNum(Math.floor(GD.getCoin())) + "";
        this.on(GDef.wdEvent.addCoin, (num, startPos) => {
            if (num) {
                console.log("num:" + num);
                let preNum = parseInt(GD.getCoin());
                GD.addCoin(num);

                if (startPos) {
                    tween(self.node).delay(0.2).call(() => {
                        self.emit(GDef.wdEvent.playEffect, "coin");
                    }).start();
                    Tool.playCoin(self.node, startPos, self.node.getChildByName("icon").position, () => {

                        self.playAni(preNum, num);
                    });
                }
                else this.playAni(preNum, num);
            }
        });
        this.on(GDef.wdEvent.updateCoin, () => {
            {
                self.labelCoinNum.string = Tool.formatNum(Math.floor(GD.getCoin())) + "";
            }
        });
    }
    playAni(preNum: any, num: any) {

        var self = this;
        if (num > 0) {
            tween(self.nodeCoin).to(0.5, { scale: v3(1, 1, 1) }, { easing: easing.elasticOut }).start();
        }
        const tweenTargetVec32 = new Vec3();
        this.tweenProgress = new Tween(tweenTargetVec32)
            .by(0.5, new Vec3(1, 1, 1), {
                'onUpdate': (target: object | undefined, ratio: number | undefined) => {
                    self.labelCoinNum.string = Tool.formatNum(Math.floor(preNum + ratio * num)) + "";
                }
            });
        this.tweenProgress.start();
    }

}
